﻿Imports System.Xml
Imports DotNetNuke.Services.Localization
Imports System.IO
Imports System.Web

Namespace DotNetNuke.Modules.ActiveForums
    Public Class LocalizationUtils
        Public Function SaveResource(ByVal name As String, ByVal resourceText As String, ByVal PortalId As Integer) As Boolean
            Dim portalResources As New XmlDocument
            Dim defaultResources As New XmlDocument
            Dim node, parent As XmlNode
            Dim filename As String

            Try
                defaultResources.Load(GetResourceFile("", Localization.SystemLocale, PortalId))
                filename = GetResourceFile("Portal", Localization.SystemLocale, PortalId)
                If File.Exists(filename) Then
                    portalResources.Load(filename)
                Else
                    portalResources.Load(GetResourceFile("", Localization.SystemLocale, PortalId))
                End If

                UpdateResourceFileNode(portalResources, name, resourceText)
                'UpdateResourceFileNode(portalResources, "GroupProperties_" + prop.PropertyName + ".Help", resourceText)
                'UpdateResourceFileNode(portalResources, "GroupProperties_" + prop.PropertyCategory + ".Header", prop.PropertyCategory)

                ' remove unmodified keys
                For Each node In portalResources.SelectNodes("//root/data")
                    Dim defaultNode As XmlNode = defaultResources.SelectSingleNode("//root/data[@name='" + node.Attributes("name").Value + "']")
                    If Not defaultNode Is Nothing AndAlso defaultNode.InnerXml = node.InnerXml Then
                        parent = node.ParentNode
                        parent.RemoveChild(node)
                    End If
                Next
                ' remove duplicate keys
                For Each node In portalResources.SelectNodes("//root/data")
                    If portalResources.SelectNodes("//root/data[@name='" + node.Attributes("name").Value + "']").Count > 1 Then
                        parent = node.ParentNode
                        parent.RemoveChild(node)
                    End If
                Next

                If portalResources.SelectNodes("//root/data").Count > 0 Then
                    ' there's something to save
                    portalResources.Save(filename)
                Else
                    ' nothing to be saved, if file exists delete
                    If File.Exists(filename) Then
                        File.Delete(filename)
                    End If
                End If
                Return True
            Catch exc As Exception    'Module failed to load
                Return False
                ' UI.Skins.Skin.AddModuleMessage(Me, Localization.GetString("Save.ErrorMessage", Me.LocalResourceFile), UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning)
            End Try
        End Function

        Public Function GetResourceFile(ByVal type As String, ByVal language As String, ByVal PortalId As Integer) As String
            Dim resourcefilename As String = "~/DesktopModules/ActiveForums/app_localresources/SharedResources.resx"
            'If IO.File.Exists(HttpContext.Current.Server.MapPath("~/admin/users/app_localresources/profile.ascx.resx")) Then
            '    resourcefilename = "~/admin/users/app_localresources/profile.ascx.resx"
            'ElseIf IO.File.Exists(HttpContext.Current.Server.MapPath("~/desktopmodules/admin/security/app_localresources/profile.ascx.resx")) Then
            '    resourcefilename = "~/desktopmodules/admin/security/app_localresources/profile.ascx.resx"
            'End If
            If language <> Localization.SystemLocale Then
                resourcefilename = resourcefilename.Substring(0, resourcefilename.Length - 5) + "." + language + ".resx"
            End If

            If type = "Portal" Then
                resourcefilename = resourcefilename.Substring(0, resourcefilename.Length - 5) + "." + "Portal-" + PortalId.ToString + ".resx"
            End If

            Return HttpContext.Current.Server.MapPath(resourcefilename)

        End Function

        Public Sub UpdateResourceFileNode(ByVal xmlDoc As XmlDocument, ByVal key As String, ByVal text As String)
            Dim node, nodeData As XmlNode
            Dim attr As XmlAttribute

            node = xmlDoc.SelectSingleNode("//root/data[@name='" + key + "']/value")
            If node Is Nothing Then
                ' missing entry
                nodeData = xmlDoc.CreateElement("data")
                attr = xmlDoc.CreateAttribute("name")
                attr.Value = key
                nodeData.Attributes.Append(attr)
                xmlDoc.SelectSingleNode("//root").AppendChild(nodeData)
                node = nodeData.AppendChild(xmlDoc.CreateElement("value"))
            End If
            node.InnerXml = HttpContext.Current.Server.HtmlEncode(text)

        End Sub
    End Class
End Namespace